//
//  DetectionViewController.m
//  FaceSDKSample_IOS
//
//  Created by 阿凡树 on 2017/5/23.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "BDFaceDetectionViewController.h"
#import <IDLFaceSDK/IDLFaceSDK.h>
#import <AVFoundation/AVFoundation.h>
#import "BDFaceImageShow.h"



@interface BDFaceDetectionViewController ()

@property (nonatomic, readwrite, retain) UIView *animaView;
@end
int remindCode = -1;
@implementation BDFaceDetectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 纯粹为了在照片成功之后，做闪屏幕动画之用
    self.animaView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.animaView.backgroundColor = [UIColor whiteColor];
    self.animaView.alpha = 0;
    [self.view addSubview:self.animaView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"进入新的界面");
    [[IDLFaceDetectionManager sharedInstance] startInitial];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[IDLFaceDetectionManager sharedInstance] reset];
}

- (void)onAppWillResignAction {
    [super onAppWillResignAction];
    [IDLFaceDetectionManager.sharedInstance reset];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)faceProcesss:(UIImage *)image {
    if (self.hasFinished) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    [[IDLFaceDetectionManager sharedInstance] detectStratrgyWithNormalImage:image previewRect:self.previewRect detectRect:self.detectRect completionHandler:^(FaceInfo *faceinfo, NSDictionary *images, DetectRemindCode remindCode) {
         switch (remindCode) {
            case DetectRemindCodeOK: {
                weakSelf.hasFinished = YES;
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"liveness_good"]];
                if (images[@"image"] != nil && [images[@"image"] count] != 0) {
                    
                    NSArray *imageArr = images[@"image"];

                    FaceCropImageInfo * bestImage = imageArr[0];
                    // UI显示选择原图，避免黑框情况
                    [[BDFaceImageShow sharedInstance] setSuccessImage:bestImage.originalImage];
                    [[BDFaceImageShow sharedInstance] setSilentliveScore:bestImage.silentliveScore];
                    
                    if (self.closeBlock) {
                        self.closeBlock(@"1", imageArr);
                    }
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (self.resultBackEnable) {
                            [weakSelf dismissViewControllerAnimated:YES completion:^{
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                    [self closeAction:nil];
                                }];
                            }];
                        } else {
                            UIView *backView = [[UIView alloc] initWithFrame:self.view.bounds];
                            backView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
                            [self.view addSubview:backView];
                            BDFaceProgressHUD *hud = [BDFaceProgressHUD showHUDAddedTo:[[UIApplication sharedApplication] delegate].window animated:YES];
                            hud.mode = BDFaceProgressHUDModeIndeterminate;
                            hud.detailsLabel.text = @"处理中，请稍后...";
                            hud.userInteractionEnabled = NO;
                        }
                    });
                }
                [self singleActionSuccess:true];
                break;
            }
            case DetectRemindCodeDataHitOne:
                 [self warningStatus:CommonStatus warning:self.tipsDictionary[@"liveness_good"]];
                 break;
            case DetectRemindCodePitchOutofDownRange:
                [self warningStatus:PoseStatus warning:self.tipsDictionary[@"detect_head_up"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodePitchOutofUpRange:
                [self warningStatus:PoseStatus warning:self.tipsDictionary[@"detect_head_down"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeYawOutofLeftRange:
                [self warningStatus:PoseStatus warning:self.tipsDictionary[@"detect_head_right"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeYawOutofRightRange:
                [self warningStatus:PoseStatus warning:self.tipsDictionary[@"detect_head_left"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodePoorIllumination:
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"detect_low_light"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeNoFaceDetected:
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"detect_face_in"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeImageBlured:
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"detect_keep"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionLeftEye:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionRightEye:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionNose:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionMouth:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionLeftContour:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionRightContour:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeOcclusionChinCoutour:
                [self warningStatus:occlusionStatus warning:self.tipsDictionary[@"detect_occ_face"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeTooClose:
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"detect_zoom_out"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeTooFar:
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"detect_zoom_in"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeBeyondPreviewFrame:
                [self warningStatus:CommonStatus warning:self.tipsDictionary[@"detect_face_in"]];
                [self singleActionSuccess:false];
                break;
            case DetectRemindCodeVerifyInitError:
                [self warningStatus:CommonStatus warning:@"验证失败"];
                break;
            case DetectRemindCodeTimeout: {
                if (self.closeBlock) {
                    self.closeBlock(@"-1", nil);
                }
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [self closeAction:nil];
                }];
                break;
            }
            case DetectRemindCodeConditionMeet: {
            }
                break;
            default:
                break;
        }
    }];
}

- (void)selfReplayFunction{
    [[IDLFaceDetectionManager sharedInstance] reset];
}
@end
